package com.wv.api.server.basic;

import cn.dev33.satoken.util.SaResult;
import com.wv.api.server.common.BaseController;
import com.wv.api.service.basic.IBasicHouseService;
import com.wv.common.result.Result;
import com.wv.model.basic.BasicHouse;
import com.wv.model.basic.condition.BasicHouseCondition;
import com.wv.model.system.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "房源管理")
@RequestMapping("/house")
public class BasicHouseController extends BaseController {

    @Autowired
    private IBasicHouseService iBasicHouseService;

    @PostMapping("/list")
    @ApiOperation("房源列表")
    public Result getList(@RequestBody BasicHouseCondition condition){
        return iBasicHouseService.getHouseList(condition);
    }

    @PostMapping
    @ApiOperation("添加房源")
    public Result addHouse(@RequestBody BasicHouse basicHouse){
        return iBasicHouseService.addHouse(basicHouse);
    }

    @PutMapping
    @ApiOperation("更新房源")
    public Result updateHouse(@RequestBody BasicHouse basicHouse){
        return iBasicHouseService.updateHouse(basicHouse);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除房源")
    public Result deleteHouse(@PathVariable String id){
        return iBasicHouseService.deleteHouse(id);
    }

    @DeleteMapping("/batch")
    @ApiOperation("批量删除房源")
    public Result deleteHouse(@RequestBody List<String> ids){
        return iBasicHouseService.deleteHouse(ids);
    }

    @GetMapping("/excelTemplate")
    @ApiOperation("下载房源导入模板")
    public void excelTemplate(HttpServletResponse response){
        outExcel(response,null, BasicHouse.class,"房源导入模板");
    }

    @GetMapping("/excel")
    @ApiOperation("房源导出")
    public void excel(HttpServletResponse response){
        Result<List> list = iBasicHouseService.getAll();
        outExcel(response,list.getData(), BasicHouse.class,"房源列表");
    }

    @PostMapping("/excel")
    @ApiOperation("房源导入")
    public Result excel(@RequestPart("file") MultipartFile file){
        List<BasicHouse> list = inputExcel(file, BasicHouse.class);
        return iBasicHouseService.addHouse(list);
    }
}
